﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
using Verse.Sound;

namespace SZ.Mech
{
    public class CompProperties_SubMechCluster : CompProperties
    {
        public int tickTime;
        public CompProperties_SubMechCluster()
        {
            compClass = typeof(SubMechCluster);
        }
    }
    public class SubMechCluster : ThingComp
    {
        private CompProperties_SubMechCluster Props => (CompProperties_SubMechCluster)props;
        public int ticksLeft;
        public IncidentParms IncidentParms;
        //public float CreatePoint;

        public override void Initialize(CompProperties props)
        {
            base.Initialize(props);
            ticksLeft = Props.tickTime;
        }
        public override string CompInspectStringExtra()
        {
            CompCanBeDormant compCanBeDormant = parent.GetComp<CompCanBeDormant>();
            if (!compCanBeDormant.Awake)
            {
                return "SendSignalOnCountdownCompTime".Translate(ticksLeft.ToStringTicksToPeriod());
            }
            return null;
        }
        public override void CompTick()
        {
            base.CompTick();
            if (parent.IsHashIntervalTick(250))
            {
                TickRareWorker();
            }
        }
        public void TickRareWorker()
        {
            CompCanBeDormant compCanBeDormant = parent.GetComp<CompCanBeDormant>();
            if (ticksLeft > 0 && parent.Spawned && !compCanBeDormant.Awake)
            {
                ticksLeft -= 250;
                if (ticksLeft <= 0)
                {
                    SoundDefOf.MechanoidsWakeUp.PlayOneShot(new TargetInfo(parent.Position, parent.Map));
                    ticksLeft = Props.tickTime;
                    CreateSubMech();
                }
            }
        }

        public void CreateSubMech()
        {
            SZ_DefOf.SubIncidentWorker_SZ.Worker.TryExecute(IncidentParms);
        }

        public override void PostExposeData()
        {
            base.PostExposeData();
            Scribe_Values.Look(ref ticksLeft, "ticksLeft", 0);
            Scribe_Deep.Look(ref IncidentParms, "incidentParms");
            //Scribe_Values.Look(ref CreatePoint, "CreatePoint", 0);
        }
    }
}
